using Unity.VisualScripting;
using UnityEngine;

public class OnGroundSensor :MonoBehaviour
{
    ///胶囊体来做地面检测
    public CapsuleCollider capsule;
    public Vector3 pointUp;
    public Vector3 pointDown;
    public float radius; //检测半径

    public Transform parent;
    public Collider[] outPutCols;

    public void Init(Transform parent)
    {
        this.capsule = parent.GetComponent<CapsuleCollider>();
        radius = capsule.radius;
        this.parent = parent.parent.parent;
        //pointUp = transform.parent.parent.position + transform.parent.parent.up * (-capsule.height / 2f + radius);
        //pointDown = transform.parent.parent.position + transform.parent.parent.up * (capsule.height / 2f - radius);

    }

    //private void Update()
    //{
    //    ColliderCheck();
    //}
    //public void Awake()
    //{
    //    this.capsule = transform.parent.Find("shenti").GetComponent<CapsuleCollider>();
             
    //    Vector3 pos = transform.parent.parent.transform.position;
    //    Transform  point = transform.parent.parent;
    //    radius = capsule.radius;
    //    //pointUp = transform.position + transform.up * (capsule.height - radius);
    //    //pointDown = transform.position + transform.up * ( radius);
    //}

    public void DoUpdate()
    {
        ColliderCheck();
    }

    public void DoLastUpdate()
    {
        
    }
    public void ColliderCheck()
    {
        Vector3 pos = this.parent.position;
        Transform point = this.parent.transform;
        pointUp = pos + point.up * (-capsule.height / 2f);
        pointDown = pos + point.up * (capsule.height / 2f);
        outPutCols = Physics.OverlapCapsule(pointUp, pointDown, radius,LayerMask.GetMask("Ground"));
        if (outPutCols.Length>0)
        {
            foreach (Collider col in outPutCols)
            {
                //Debug.Log($"col{col}");
            }
            
        }
    }


    public bool isOnGround()
    {
        return outPutCols.Length != 0;
    }



}
